//
//  CartoonClassifyViewController.m
//  Double
//
//  Created by 袁庆秋 on 15/5/23.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "CartoonClassifyViewController.h"
#import <AFNetworking/AFHTTPRequestOperationManager.h>
#import <UIImageView+WebCache.h>
#import "CartoonClassifyCell.h"
#import "CartoonClassifyModel.h"
#import "CartoonClassifyReusableVIew.h"
#import "ChaseViewController.h"
#import "CartoonClassifyMainViewController.h"
#import "CartoonClassifyNewViewController.h"
#import "CartoonClassifyHotViewController.h"
#import "CartoonClassifyGoodViewController.h"


@interface CartoonClassifyViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain) NSArray *array;
@property (nonatomic, retain) NSMutableArray *smallArray1;
@property (nonatomic, retain) NSMutableDictionary *dic;
@end

@implementation CartoonClassifyViewController

- (void)dealloc
{
    [_dic release];
    [_smallArray1 release];
    [_array release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSArray array];
        self.smallArray1 = [NSMutableArray array];
        self.dic = [NSMutableDictionary dictionary];
    }
    return self;
}

#pragma mark -
#pragma mark 网络请求
- (void)startConnection
{
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Cartoon&ios=23&pad=0&a=channelgroupNew"];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        self.array = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
//                NSLog(@"%@", _array);
        
        for (NSDictionary *dic in self.array) {
            self.dic = [NSMutableDictionary dictionary];
            [_dic addEntriesFromDictionary:dic];
//                        NSLog(@"%@", _dic);
            
            NSString *str = [self.dic objectForKey:@"group"];
            [self.smallArray1 addObject:str];
        
            
            
            
        }
        
        [self.collection reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

#pragma mark -
#pragma mark 定义collectionView
- (void)myCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width / 3 - 20, 140);
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    
    layout.headerReferenceSize = CGSizeMake(10, 30);
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100) collectionViewLayout:layout];
    
    _collection.dataSource = self;
    _collection.delegate = self;
    
    [self.collection setBackgroundColor:[UIColor whiteColor]];
    
    [self.collection registerClass:[CartoonClassifyCell class] forCellWithReuseIdentifier:@"cell"];
    //注册section标题和结尾的重用池
    
    [self.collection registerClass:[CartoonClassifyReusableVIew class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.view addSubview:_collection];
    [layout release];
    [_collection release];
}
#pragma mark -
#pragma mark collection协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSMutableDictionary *dic = [self.array objectAtIndex:section];
    NSMutableArray *array = [dic objectForKey:@"list"];
    return [array count];
    
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CartoonClassifyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSMutableDictionary *dic1 = [self.array objectAtIndex:indexPath.section];
    NSMutableArray *array = [dic1 objectForKey:@"list"];
    NSMutableArray *bigArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        CartoonClassifyModel *model = [[CartoonClassifyModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [bigArray addObject:model];
        
    }
    CartoonClassifyModel *model = [bigArray objectAtIndex:indexPath.item];
    
    cell.priceLable.text = model.name;
    [cell.myImage sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:nil options:SDWebImageRetryFailed];
    
    return cell;
}

#pragma mark -
#pragma mark section标题

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return self.array.count;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    CartoonClassifyReusableVIew *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    header.myLable.text = [self.smallArray1 objectAtIndex:indexPath.section];
    return header;
}
#pragma mark -
#pragma mark cell点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    ChaseViewController *chase = [[ChaseViewController alloc] init];
    
    NSMutableDictionary *dic1 = [self.array objectAtIndex:indexPath.section];
    NSMutableArray *array = [dic1 objectForKey:@"list"];
    NSMutableArray *bigArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        CartoonClassifyModel *model = [[CartoonClassifyModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [bigArray addObject:model];
        
    }
    CartoonClassifyModel *model = [bigArray objectAtIndex:indexPath.item];
    
    CartoonClassifyMainViewController *mainView = [[CartoonClassifyMainViewController alloc] init];

    if (indexPath.section == 0) {
        if (indexPath.item == 0) {
            [self.navigationController pushViewController:chase animated:YES];
            chase.navigationItem.title = @"追番表";
        }
        else {
            mainView.cartoonNew.myID1 = model.ManHuaId;
            mainView.cartoonHot.myID2 = model.ManHuaId;
            mainView.cartoonGood.myID3 = model.ManHuaId;
            [self.navigationController pushViewController:mainView animated:YES];
        }

    } else {
        mainView.cartoonNew.myID1 = model.ManHuaId;
        mainView.cartoonHot.myID2 = model.ManHuaId;
        mainView.cartoonGood.myID3 = model.ManHuaId;
        [self.navigationController pushViewController:mainView animated:YES];
    }
    
    [chase release];
    [mainView release];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self myCollectionView];
    [self startConnection];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
